package com.xiaren.util;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.ParseException;
import org.apache.http.client.CookieStore;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.cookie.Cookie;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 建议http
 */
public class SimpleHttpClient {

    public static String before_url = "";

    private static CloseableHttpClient httpClient;

    private static CookieStore cookieStore;

    static {
        cookieStore = new BasicCookieStore();

        // 将CookieStore设置到httpClient中
        httpClient = HttpClients.custom().setDefaultCookieStore(cookieStore).build();
    }

    public static CookieStore addCookie(Cookie cookie) {
        cookieStore.addCookie(cookie);
        return cookieStore;
    }

    public static CookieStore getCookieStore() {
        return cookieStore;
    }

    public static String getCookie(String name) {
        List<Cookie> cookies = cookieStore.getCookies();
        for (Cookie cookie : cookies) {
            if (cookie.getName().equalsIgnoreCase(name)) {
                return cookie.getValue();
            }
        }
        return null;

    }

    /**
     * GET 请求
     *
     * @param url
     * @return
     * @throws IOException
     */
    public static String get(String url) throws IOException {
        return get(url, null);
    }

    /**
     * GET 请求
     *
     * @param url
     * @return
     * @throws IOException
     */
    public static HttpResponse getResponse(String url) throws IOException {
        return getResponse(url, null);
    }

    /**
     * GET 请求
     *
     * @param url
     * @return
     * @throws IOException
     */
    public static String get(String url, Map<String, String> headers) throws IOException {
        HttpResponse httpResponse = getResponse(url, headers);
        return EntityUtils.toString(httpResponse.getEntity());
    }

    /**
     * GET请求，返回response
     *
     * @param url
     * @param headers 头部
     * @return
     * @throws IOException
     */
    public static HttpResponse getResponse(String url, Map<String, String> headers) throws IOException {
        url = before_url + url;
        HttpGet httpGet = new HttpGet(url);
        if (headers != null) {
            addHeaders(httpGet, headers);
        }
        HttpResponse httpResponse = httpClient.execute(httpGet);
        return httpResponse;
    }

    /**
     * POST 请求
     *
     * @param url
     * @param params
     * @param headers
     * @return
     * @throws ParseException
     * @throws IOException
     */
    public static HttpResponse postResponse(String url, Map<String, Object> params, Map<String, String> headers) throws ParseException, IOException {
        url = before_url + url;
        HttpPost httpPost = new HttpPost(url);
        UrlEncodedFormEntity postEntity = new UrlEncodedFormEntity(getParam(params), "UTF-8");
        httpPost.setEntity(postEntity);
        if (headers != null) {
            addHeaders(httpPost, headers);
        }
        HttpResponse httpResponse = httpClient.execute(httpPost);
        return httpResponse;
    }

    /**
     * POST 请求
     *
     * @param url
     * @param params
     * @param headers
     * @return
     * @throws ParseException
     * @throws IOException
     */
    public static String post(String url, Map<String, Object> params, Map<String, String> headers) throws ParseException, IOException {
        HttpResponse httpResponse = postResponse(url, params, headers);
        return EntityUtils.toString(httpResponse.getEntity());
    }

    /**
     * POST 请求
     * @param url
     * @param params
     * @return
     * @throws ParseException
     * @throws IOException
     */
    public static HttpResponse postResponse(String url, Map<String, Object> params) throws ParseException, IOException {
        return postResponse(url, params, null);
    }

    /**
     * POST 请求
     * @param url
     * @param params
     * @return
     * @throws ParseException
     * @throws IOException
     */
    public static String post(String url, Map<String, Object> params) throws ParseException, IOException {
        HttpResponse httpResponse = postResponse(url, params);
        return EntityUtils.toString(httpResponse.getEntity());
    }

    /**
     * 参数
     *
     * @param parameterMap
     * @return
     */
    private static List<NameValuePair> getParam(Map<String, Object> parameterMap) {
        List<NameValuePair> param = new ArrayList<NameValuePair>();
        if(parameterMap == null){
            return param;
        }
        for (Map.Entry<String, Object> entry : parameterMap.entrySet()) {
            param.add(new BasicNameValuePair(entry.getKey(), String.valueOf(entry.getValue())));
        }
        return param;
    }

    /**
     * 添加头
     *
     * @param httpRequest
     * @param headerMap
     */
    private static void addHeaders(HttpRequestBase httpRequest, Map<String, String> headerMap) {
        for (Map.Entry<String, String> entry : headerMap.entrySet()) {
            httpRequest.addHeader(entry.getKey(), entry.getValue());
        }
    }


    public static HttpResponse deleteResponse(String url) throws ParseException, IOException {
        url = before_url + url;
        HttpDelete httpPost = new HttpDelete(url);
        HttpResponse httpResponse = httpClient.execute(httpPost);
        return httpResponse;
    }

}